import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import {
  isToken
} from '../utils/auth'

Vue.use(VueRouter)

const routes = [{
  path: '/',
  name: 'Home',
  component: () => import('../views/Home.vue')
},
{
  path: '/Login',
  name: 'Login',
  component: () => import('../views/Login.vue'),
},
{
  path: '/Register',
  name: 'Register',
  component: () => import('../views/register.vue')
},

{
  path: '/Order',
  name: 'Order',
  component: () => import('../views/Order.vue'),
  meta: {
    needLogin: true
  },

},
{
  path: '/User',
  name: 'User',
  component: () => import('../views/User.vue'),
  meta: {
    needLogin: true
  }
},
{
  path: '/Find',
  name: 'Find',
  component: () => import('../views/Find.vue')
},
{
  path: '/Car',
  name: 'Car',
  component: () => import('../views/Car.vue'),
  meta: {
    needLogin: true
  }
},
{
  path: '/Home/SortList',
  name: 'SortList',
  component: () => import('../views/SortList.vue')
},

{
  path: '/Details',
  name: 'Details',
  component: () => import('../views/Details.vue'),
},
{
  path: "/SetUp",
  name: "SetUp",
  component: () => import("../views/SetUp.vue")
},
{
  path: "/CarCopy",
  name: "CarCopy",
  component: () => import("../views/CarCopy.vue")
},
{
  path: "/Address",
  name: "Address",
  component: () => import("../views/Address.vue")
},
{
  path: "/AddressList",
  name: "AddressList",
  component: () => import("../views/AddressList.vue")
},
{
  path: "/AddressModify",
  name: "AddressModify",
  component: () => import("../views/AddressModify.vue")
},
{
  path: '/*',
  name: 'PNF',
  component: () => import('../views/404.vue')
},


]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.meta.needLogin) {
    if (isToken()) {
      next()
    } else {
      next({
        name: 'Login'
      })
    }
  } else {
    next()
  }
})

export default router